package com.softtown.modules.utils;

import com.softtown.modules.organization.entity.CompOrgDeptEntity;

import java.util.List;
import java.util.Map;

/**
 * 获取上级部门ids
 *
 * @author Dingyayong
 * @version v1.0
 * @create 2019-08-22 18:00
 **/
public class GetFathersIdByDept {
    /**
     * 获取上级部门
     * @param pid
     * @param compOrgDeptEntityList
     * @param map
     * @return String
     */
    public Map<String, String> getIds (Long pid, List<CompOrgDeptEntity> compOrgDeptEntityList, Map<String, String> map) {
        String ids = map.get("ids");
        String pnames = map.get("pnames");
        for (int i = 0; i < compOrgDeptEntityList.size(); i++) {
            if (compOrgDeptEntityList.get(i).getId().equals(pid)) {
                ids += "," + compOrgDeptEntityList.get(i).getPid();
                pnames += "," + compOrgDeptEntityList.get(i).getName();
                map.put("ids", ids);
                map.put("pnames", pnames);
                if (compOrgDeptEntityList.get(i).getPid() == 0) {
                    return map;
                }
                map = getIds(compOrgDeptEntityList.get(i).getPid(), compOrgDeptEntityList, map);
                break;
            }
        }
        return map;
    }

    /**
     * 获取预算部门
     * @param compOrgDeptEntityList
     * @param pid
     * @return CompOrgDeptEntity getBudget
     */
    public CompOrgDeptEntity getBudget (List<CompOrgDeptEntity> compOrgDeptEntityList, Long pid) {
        CompOrgDeptEntity compOrgDeptEntity = new CompOrgDeptEntity();
        for (int i = 0; i < compOrgDeptEntityList.size(); i++) {
            if (compOrgDeptEntityList.get(i).getId().equals(pid)) {
                if (compOrgDeptEntityList.get(i).getIsBudDept() == 1) {
                    return  compOrgDeptEntityList.get(i);
                }
                return getBudget(compOrgDeptEntityList, compOrgDeptEntityList.get(i).getPid());
            }
        }
        return compOrgDeptEntity;
    }
}

